package com.mdh.runerrands.controller;

import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.Product;
import com.mdh.runerrands.pojo.ProductDetail;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/controller/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @ApiOperation("批量删除产品信息")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int  ...ids){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            int b = productService.batchDelete(ids);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("通过ID删除产品信息")
    @RequestMapping(value = "/deleteById" ,method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = productService.deleteById(id);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("通过categoryId查询产品信息")
    @RequestMapping(value = "/findByCategoryId",method = RequestMethod.GET)
    public Result findByCategoryId(int categoryId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            result.setData(productService.findByCategoryId(categoryId));
            result.setMessage("success");
            return  result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("通过ID查询产品信息")
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            result.setMessage("success");
            result.setData(productService.findById(id));
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }

    }
    @ApiOperation("分页查询产品信息")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Result query(int page ,int pageSize,String name ,String description,String price,String status){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            QueryPage queryPage = new QueryPage();
            List<Product> query = productService.query(page, pageSize, name, description, price, status);
            queryPage.setList(query);
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            queryPage.setTotal(productService.selectCount(name,description,price,status));
            result.setMessage("success");
            result.setData(queryPage);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;

        }
    }
    @ApiOperation("分页查询产品信息级联栏目")
    @RequestMapping(value = "/queryProductCascadeCategory",method = RequestMethod.POST)
    public Result queryProductCascadeCategory(int page ,int pageSize,String name ,String description,String price,String status){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            QueryPage queryPage = new QueryPage();
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            List<ProductDetail> productDetails = productService.queryDetail(page, pageSize, name, description, price, status);
            queryPage.setList(productDetails);
            queryPage.setTotal(productService.selectCount(name,description,price,status));
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;

        }
    }
    @ApiOperation("保存或更新产品信息")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdate(Product product){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            if (product != null &&product.getId() != 0){
                boolean update = productService.update(product);
                result.setData(update);
                result.setMessage("success");
                return result;
            }else {
                boolean insert = productService.insert(product);
                result.setMessage("success");
                result.setData(insert);
                return result;
            }
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
}
